﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace ReadeAssetManager
{
    public partial class ucLogin : UserControl
    {
        private AMSServiceRef.User _user;
        private delegateReturn _returnaction;

        private const string STR_SUCCESS = "Login Successful!";
        private const string STR_FAIL = "Access denied.";
        private const string STR_LOGOUT = "Logout Successful!";

        /// <summary>
        /// CurrentUser
        /// Property getter to expose the currently logged in user (unless null) to frmMain.
        /// </summary>
        public AMSServiceRef.User CurrentUser
        {
            get { return _user; }
        }

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ReturnAction">[delegateReturn] Delegate method called on frmMain to inform
        /// the main form that an event related to login has occurred.</param>
        /// <param name="CurrentUser">[AMSServiceRef.User] The current application user.  This value is
        /// an instace of the object if logging out or null if logging in.</param>
        public ucLogin(delegateReturn ReturnAction, AMSServiceRef.User CurrentUser)
        {
            InitializeComponent();
            _returnaction = ReturnAction;
            _user = CurrentUser;
            if (_user != null)
            {
                EnabledForLogin(false);
                txtUserName.Focus();
            }
            else
                EnabledForLogin(true);
        }

        /// <summary>
        /// Login
        /// Invokes a service method to log in this user and pull all asset data for this user.
        /// </summary>
        /// <returns>[bool] True if login was successful, otherwise false.</returns>
        public bool Login()
        {
            AMSServiceRef.AMSServiceClient client = new ReadeAssetManager.AMSServiceRef.AMSServiceClient();
            try
            {
                _user = client.Login(txtUserName.Text, txtPassword.Text);
                return _user != null;
            }
            catch (CommunicationException cex)
            {
                MessageBox.Show("Communication Error: " + cex.Message + "\n" + cex.InnerException.ToString());
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                return false;
            }
        }

        /// <summary>
        /// btnLogin_Click
        /// This event handler covers both logging in and logging out.  The button tag contains
        /// a boolean value to indicate which action to perform (true if login; false if logout).
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if ((bool)btnLogin.Tag)
            {
                if (Login())
                {
                    lblLoginResult.Text = STR_SUCCESS;
                    EnabledForLogin(false);
                }
                else
                {
                    lblLoginResult.Text = STR_FAIL;
                }
            }
            else
            {
                _user = null;
                lblLoginResult.Text = STR_LOGOUT;
                EnabledForLogin(true);
            }

            _returnaction(this);
        }

        /// <summary>
        /// EnableForLogin
        /// Modifies the ucLogin control to display the appropriate controls for log in or
        /// log out.
        /// </summary>
        /// <param name="Enabled">[bool] True if setting up for login, otherwise false.</param>
        private void EnabledForLogin(bool Enabled)
        {
            btnLogin.Tag = Enabled;
            lblTitle.Text = Enabled ? "Log In" : "Log Out";
            lblUserName.Visible = Enabled;
            txtUserName.Visible = Enabled;
            lblPassword.Visible = Enabled;
            txtPassword.Visible = Enabled;
            if (Enabled)
                btnLogin.Image = global::ReadeAssetManager.Properties.Resources.loginicon;
            else
                btnLogin.Image = global::ReadeAssetManager.Properties.Resources.logout;            
        }
    }
}
